<?php

namespace App\Models;

class Roles extends Base
{
    protected $table = 'roles';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];

    protected $fillable = [
        'id','name','status','description','rules','created_at','updated_at',
    ];

    protected $casts = ['rules' => 'array'];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
            if(isset($params['kwd']) && $params['kwd']) {
                $query ->where('name','like','%' . $params['kwd'] . '%');
            }
            if(isset($params['status']) && $params['status']) {
                $query ->where('status',$params['status']);
            }
        })->orderBy('created_at', 'desc')
            ->paginate($limit);
        $this->stateToText($lists,[
            'status' => self::STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 获取当前登录管理员的权限
     * @return mixed
     */
    protected function adminRole(){
        return $this->find(auth()->user()->role_id)->rules;
    }



}
